var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/f77_two',(err)=>{
    if(err){
    console.log(err);
    return;
    }
    console.log("连接成功")
});
//1 创建数据结构
//学生
let studentSchema = new mongoose.Schema({
    name:String,
    gender:String,
    age:Number,
    //该字段是需要被填充的字段
    classid:{
        type: mongoose.Schema.Types.ObjectId,
        ref:"classModel"     //引用那个数据模型
    }
})
//班级数据结构
let classSchema = new mongoose.Schema({
     name:String,
     nums:Number,
     createTime:Date
})
//创建数据模型
let studentModel =mongoose.model("studentModel",studentSchema,"students");
let classModel =mongoose.model("classModel",classSchema,"classes");
//非连表查询
//let studentsRes = studentModel.find()
//连表查询 populate: 填充的字段 classid
let studentsRes = studentModel.find().populate("classid")
studentsRes.then((data)=>{
    //console.log(data[0].classid._id,data[0].classid.name)
    console.log(data);
})